package com.freewater.cargotracker.infrastructure.persistence.mybatis.converter;

import com.freewater.cargotracker.domain.model.cargo.Leg;
import com.freewater.cargotracker.infrastructure.persistence.mybatis.dbobject.LegPO;

public class LegConverter {

    public static LegPO serialize(Long cargoId, Leg leg) {
        if (leg == null) {
            return null;
        }
        LegPO legPO = new LegPO();
        legPO.setId(leg.getId());
        legPO.setCargoId(cargoId);
        legPO.setVoyageId(leg.getVoyageId());
        legPO.setLoadLocation(LocationConverter.serialize(leg.getLoadLocation()));
        legPO.setUnloadLocation(LocationConverter.serialize(leg.getUnloadLocation()));
        legPO.setLoadTime(leg.getLoadTime());
        legPO.setUnloadTime(leg.getUnloadTime());
        return legPO;
    }

    public static Leg deserialize(LegPO legPO) {
        if (legPO == null) {
            return null;
        }
        Leg leg = new Leg(legPO.getVoyageId(),
                LocationConverter.deserialize(legPO.getLoadLocation()),
                LocationConverter.deserialize(legPO.getUnloadLocation()),
                legPO.getLoadTime(),
                legPO.getUnloadTime());
        leg.setId(legPO.getId());
        return leg;
    }
}
